package com.small.account.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.common.basis.result.Result;
import com.common.basis.result.ResultPage;
import com.common.basis.result.ResultSet;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.small.account.entity.AuDic;
import com.small.account.entity.vo.AuDicQ;
import com.small.account.service.IAuDicService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 字典
 *
 * @author wangguochao
 * @auther W.GC
 * @date 2021-01-28
 */
@RestController
@AllArgsConstructor
@RequestMapping("/au-dic")
public class AuDicController {


    private final IAuDicService iAuDicService;


    /**
     * 查询全部
     */
    @GetMapping()
    public ResultPage<AuDic> findAll(AuDicQ param) {
        QueryWrapper<AuDic> wrapper = new QueryWrapper<AuDic>();
        wrapper.lambda().eq(param.getId() != null, AuDic::getId, param.getId());
        wrapper.lambda().orderByDesc(AuDic::getCreated);
        IPage<AuDic> iPage = iAuDicService.page(param.page(), wrapper);
        return ResultSet.page(iPage);
    }


    /**
     * 保存-新增
     * @undone
     */
    @PostMapping()
    public Result<?> save(@RequestBody @Validated AuDic param) {
        LambdaQueryWrapper<AuDic> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AuDic::getGroupCode, param.getGroupCode())
                .eq(AuDic::getDicKey, param.getDicKey());
        int count = iAuDicService.count(wrapper);

        if (count > 0) {
            return ResultSet.error("该类型已经存在");
        }
        boolean b = iAuDicService.save(param);
        return ResultSet.operation(b);
    }

    /**
     * 保存-修改
     */
    @PutMapping()
    public Result<?> modify(@RequestBody @Validated AuDic param) {
        LambdaQueryWrapper<AuDic> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AuDic::getGroupCode, param.getGroupCode())
                .eq(AuDic::getDicKey, param.getDicKey())
                .ne(AuDic::getId, param.getId());
        int count = iAuDicService.count(wrapper);
        if (count > 0) {
            return ResultSet.error("该类型已经存在");
        }
        boolean b = iAuDicService.updateById(param);
        return ResultSet.operation(b);
    }

    /**
     * 删除
     */
    @DeleteMapping()
    public Result<?> del(@RequestParam String id) {
        boolean b = iAuDicService.removeById(id);
        return ResultSet.operation(b);
    }

    /**
     * 分组查询
     */
    @GetMapping("findList")
    public Result<Map<String, List<AuDic>>> findList() {
        List<AuDic> list = iAuDicService.list();
        Map<String, List<AuDic>> collect = list.stream().collect(Collectors.groupingBy(AuDic::getGroupCode));
        return ResultSet.go(collect);
    }
}

